<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      input {
        outline: none;
      }
    </style>
  </head>
  <body>
    <input type="text" name="" id="" />
    <button class="set">存储数据</button>
    <button class="get">获取数据</button>
    <button class="remove">删除数据</button>
    <button class="del">清空数据</button>
    <script>
      console.log(localStorage.getItem("name"));
      // 1、生命周期为关闭浏览器窗口
      // 2、在同一个窗口页面下数据可共享
      // 3.以键值对的形式存储使用
      var set = document.querySelector(".set");
      var ipt = document.querySelector("input");
      var get = document.querySelector(".get");
      var remove = document.querySelector(".remove");
      var del = document.querySelector(".del");
      set.addEventListener("click", function () {
        var val = ipt.value;
        //存储数据
        sessionStorage.setItem("uname", val);
        sessionStorage.setItem("pwd", val);
      });
      get.addEventListener("click", function () {
        //获取数据
        console.log(sessionStorage.getItem("uname"));
      });
      remove.addEventListener("click", function () {
        //删除数据
        sessionStorage.removeItem("uname");
      });
      del.addEventListener("click", function () {
        sessionStorage.clear();
      });
    </script>
  </body>
</html>
